package com.tang.gulimall.order.config;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.tang.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000119608211";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCOVMNaOTHJSuOGjJULDCpZ/mfuNexcr5X0gPGAW/dJlKZWUl2G9LRzQuJEIJe8q8Vg40/NQO2LzdRIg64lAcwv5YBtjXX18RybYmfXFalRF3b8k5ToZ8+oQVxREB+M7KvGbQMzQOuK9OdtLNK6P7CDqOWxygQ420ZuZJvrl9FRextcjWLgjodhcZkVQ69gCaUGFsyQW4yq79Ky+mpL4yA9KUFXnNzNxJAMTWwhxInkFpTc5K1z7UI8Qv6t1i83tjEBtAzok5AwNTkcps1ugh6AU9SGbqPe7gmhBeO6zjZm02P06AXFEQ/P0WJLaemUC7BfJFY2VBJvMiYtT9tKYV85AgMBAAECggEAJnAnzd7yXh1WcwjWW4Ruj51QLhnr+mAFDGIiVnr/vqRhg6B816yQayxd9O4kbHSrOm5JJ/Jn9MCimu7NAfuJTEobs/pnKAN0qVYbGxAjiN3gaXfl2BnpD9SO/Olwy9tCzLS2D4px0jYvpeFcC6aVogjj927KUxyM2dVb8nUut2UylPInkCeg8d68GMlTXFFy92zvjZNXQVeenCh1/hjDcsv5i/HMI9O41W3MFsoOsh1gruNPBNfM7h4JraDYuRJAHk4+GwcF4ymvlC4WjjR9ce8L7DZJSm7Y9bs05jnV/mssHrpYI+VQEm5TgnrzKPoSDMa0CSdnHKq5dALEV+uw8QKBgQDdQuFMWjQRtvz7Sovtr0HMXPfO48AdLeQARki6kC1ngFu88yTFe+eIqxDNgrmULSVX/cw/NOKtW/OYpWUdKkWMLZ+lVXq+pdqx6VPUzN68VV8WfUp03rVaNkiMigN7yDZ82llRgKlBZTDqzQrN9KQllK3n0qaXhBaGvFYB4/uHjQKBgQCkrXSnesAY7ef/NucyW0WMtiPhTYHdjOLcP7lkxbGQA1LXmwXPHtYIehdpFMdGesj8tHSVLnBtMBvoyxAc1FYOXWk7BXK+NrbE9s+n0PbNjryYRCbd3IHwr16MWrZyeBBN0JEfH9zV9L0bRfvcSRRUb+Coghbcl+mtM1OqhsDlXQKBgQDUGTNthJ6bSKVzof9GxhS1+XyXN51Gm2IPnVBEfsb3i4TlONLL+D13tjwyNJj+4NQGaEUtZd3RPu7hKdMmxQnMGUQLMkiqjsR4fWmd+RGeyduP7GS+aWrA4n5jMj2qn78Tqh+q0EHvbTgaLJmp2lSrrb2TF0wYvEb3u3xa7ZeFVQKBgFWhuPpqE0AjXjNROnHuEkF0aT38Tt6FJ2a+YEptnsKzZ72j6CZm84de7ZN/8YvNx3vY3pLAnke475pjFZLYO5OmsVS2pY8R5oKPkTXDrMMBSUKolclio9Mjbjfv59WoZ1n/badQq0Y6yKdJAgnvmnm534ngc8Dr4eBlw0j2X5IlAoGAO3w1rDkFQxc1RhBliUWu3coWyJVVD/GJ0nx3F3SKAl+JAi3ikiX2v1j8v7qI0r2DPxdSj2v5oADAOuNzM05lJ1vpqdSd5TMEfWPP3jCnK3oI1Cg1jIZtkztFzpfLQHu+0cM/EyApX8VtAFUoniyI6c+5wBab53Fim79NJFCYjpg=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApNC2F6CFybOSe4JCYcphcBler8yZhFVzCcGv6BizokgliwEvAvn2PpK7Lf6BW8dHFc5g9UCjZH0gB2JNdZlEXhbefj02POb6XQ548PWLodffgJKR6DhI3GBTbZRH29AzwhhM3BURl7a1P0wQOpD6MxU5zlBQEy9RiNYKcSex9hiqK4lKv7xTviv3mqfdtZ2TWFBM0l4zUIHNzBmbabQXuTeijIQx+S87aPsbztpgWQV4+XWiBiLOYvwtzFmlza9cRJyALtxCFwQkQ03iJF4mXlmsLoKzPwrwD8Fbglm6iEog3tyL0s9GxRjKkIHZg23UkRz8Ft7aeSKPu2HCNrEHrQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://abcdefg.vaiwan.com/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }


    /**
     * 手动关单
     * @param trade_no
     * @throws AlipayApiException
     */
    public void closeOrder(String trade_no) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, app_id, merchant_private_key, "json", charset, alipay_public_key, sign_type);
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("trade_no", trade_no);
        request.setBizContent(bizContent.toString());
        AlipayTradeCloseResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }
}
